package me.zhengjie.ppService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.pojo.PPMoveMissionParameter;
import me.zhengjie.pojo.Response;
import me.zhengjie.pojo.sqlServer.PPInventory;
import me.zhengjie.pojo.sqlServer.PPMoveMission;
import me.zhengjie.pojo.sqlServer.PPWmsTask;
import me.zhengjie.repository.PPInventoryRepository;
import me.zhengjie.repository.PPMoveMissionRepository;
import me.zhengjie.service.PPStorageService;
import me.zhengjie.util.UniqueCodeUtil;
import me.zhengjie.wcs.PPWmsToWcs;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @Author: xinglin
 * @CreateTime: 2024-08-15
 * @Description: PP移库管理
 * @Version: 1.0
 */


@Slf4j
@Component
@RequiredArgsConstructor
public class PPMoveService {

    private final PPWmsToWcs wmsToWcs;
    private final PPMoveMissionRepository ppMoveMissionRepository;
    private final PPStorageService ppStorageService;
    private final PPInventoryRepository ppInventoryRepository;

    /**
     * 生成移库任务
     */
    @Transactional
    public void createPPMoveMission(PPMoveMissionParameter ppMoveMissionParameter) {

        // 根据载盘码查询库存
        PPInventory ppInventory = ppInventoryRepository.findPPInventoryByPalletCode(ppMoveMissionParameter.getPalletCode());

        // 锁储位
        ppStorageService.lockStorage(ppMoveMissionParameter.getSourceStorageCode());
        ppStorageService.lockStorage(ppMoveMissionParameter.getDestinationStorageCode());

        // 锁库存
        ppInventory.setLockStatus(0);
        ppInventoryRepository.save(ppInventory);

        PPMoveMission ppMoveMission = new PPMoveMission();
        ppMoveMission.setMissionCode(UniqueCodeUtil.createCode());
        ppMoveMission.setPriority(0);
        ppMoveMission.setMissionStatus(1);
        ppMoveMission.setPalletCode(ppMoveMissionParameter.getPalletCode());
        ppMoveMission.setSource(ppMoveMissionParameter.getSourceStorageCode());
        ppMoveMission.setDestination(ppMoveMissionParameter.getDestinationStorageCode());
        ppMoveMission.setCreateTime(LocalDateTime.now());

        ppMoveMissionRepository.save(ppMoveMission);


    }

    /**
     * 生成发送到wcs的移库任务
     *
     * @param ppMoveMission 移库任务
     * @return
     */
    public PPWmsTask getPPWmsTask(PPMoveMission ppMoveMission) {
        PPWmsTask wmsTask = new PPWmsTask();
        wmsTask.setLogicalAddressType(1);
        wmsTask.setWmsTaskId(ppMoveMission.getMissionCode());
        wmsTask.setLpnCode(ppMoveMission.getPalletCode());
        wmsTask.setLpnType(1);
        //设置工作类型为移库任务
        wmsTask.setWcsTaskType(3);
        wmsTask.setAisle(1);
        wmsTask.setFromLocation(ppMoveMission.getSource());
        wmsTask.setToLocation(ppMoveMission.getDestination());
        wmsTask.setLevel(ppMoveMission.getPriority());
        wmsTask.setStatus(ppMoveMission.getMissionStatus());
        wmsTask.setCreated(ppMoveMission.getCreateTime());
        return wmsTask;
    }


    @Transactional
    public Response sendMoveTask(PPMoveMission ppMoveMission) {

        PPWmsTask ppWmsTask = getPPWmsTask(ppMoveMission);
        updateAfterSendTask(ppWmsTask);
        return wmsToWcs.sendTask(ppWmsTask);

    }


    @Transactional(rollbackFor = Exception.class)
    public void updateAfterSendTask(PPWmsTask wmsTask) {
        ppMoveMissionRepository.updateStatusByMissionCode(String.valueOf(wmsTask.getWmsTaskId()), 2);
    }


    public PPMoveMission findNewMoveMission() {
        //避免get(0)发生空指针异常
        if (ppMoveMissionRepository.findNewMoveMission().size() > 0) {
            return ppMoveMissionRepository.findNewMoveMission().get(0);
        }
        else {
            return null;
        }
    }


}
